﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffPDA
{
    /*
     * Class: SubOn
     * 
     * Use: Form displayed when a player is being bought onto the pitch as part of
     *      a substitution
     *      
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */      
    public partial class SubOn : Form
    {
        //Details required to connect to the database
        string conString;
        SqlCeConnection conn;
        
        //The current match object
        Match theMatch;

        //id of the current match
        int matchId;

        //Buttons where the text needs to be changed to reflect the new
        //player being bought onto the pitch
        public Button subOffButton;
        public Button goalButton;
        public Button cautionButton;
        public Button sendOffButton;

        //id values from database of the player going off and the player coming on
        public int playerOff_id;
        int playerOn_id;

        //Button text for the player going off and the player coming on
        public String playerOff_text;
        String playerOn_text;

        public SubOn(Match match)
        {
            InitializeComponent();
            theMatch = match;
        }

        //This method carries out the actual substitution within the database
        private void subPlayer()
        {
            connectToDatabase();
            matchId = theMatch.match_id;

            //Current value of match timer
            String timeOfIncident = theMatch.tenMinutes + theMatch.minutes + ":" + theMatch.tenSeconds + theMatch.seconds;
            
            //Create and execute SQL command to indicate that the relevant player has been substituted off
            string offCommand = @"UPDATE player SET on_pitch=0 WHERE player_id=@player_off;";
            using (SqlCeCommand sub_off = new SqlCeCommand(offCommand, conn))
            {
                sub_off.Parameters.AddWithValue("@player_off", playerOff_id);
                sub_off.ExecuteNonQuery();
            }

            //Create and execute SQL command to indicate that the relevant player has been bought onto pitch
            string onCommand = @"UPDATE player SET on_pitch=1 WHERE player_id=@player_on;";
            using (SqlCeCommand sub_on = new SqlCeCommand(onCommand, conn))
            {
                sub_on.Parameters.AddWithValue("@player_on", playerOn_id);
                sub_on.ExecuteNonQuery();
            }

            //Create and execute SQL command to create record of the substitution
            string subCommand = @"INSERT INTO sub(player_off_id,player_on_id,time,match_id) VALUES(@playerOff,@playerOn,@time,@matchID);";
            using (SqlCeCommand sub = new SqlCeCommand(subCommand, conn))
            {
                sub.Parameters.AddWithValue("@playerOff", playerOff_id);
                sub.Parameters.AddWithValue("@playerOn", playerOn_id);
                sub.Parameters.AddWithValue("@time", timeOfIncident);
                sub.Parameters.AddWithValue("@matchID", matchId);
                sub.ExecuteNonQuery();
            }

            //Disconnect from database
            closeDatabaseConnection();
        }

        //This code is run when the first player button on the screen is clicked
        private void SubOnPlayer1Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer1Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer1Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();            

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer1Btn.Tag = playerOff_id;
            this.SubOnPlayer1Btn.Text = playerOff_text;
            this.SubOnPlayer1Btn.Enabled = false;
        }

        //This code is run when the second player button on the screen is clicked
        private void SubOnPlayer2Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer2Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer2Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer2Btn.Tag = playerOff_id;
            this.SubOnPlayer2Btn.Text = playerOff_text;
            this.SubOnPlayer2Btn.Enabled = false;
        }

        //This code is run when the third player button on the screen is clicked
        private void SubOnPlayer3Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer3Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer3Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer3Btn.Tag = playerOff_id;
            this.SubOnPlayer3Btn.Text = playerOff_text;
            this.SubOnPlayer3Btn.Enabled = false;
        }

        //This code is run when the fourth player button on the screen is clicked
        private void SubOnPlayer4Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer4Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer4Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer4Btn.Tag = playerOff_id;
            this.SubOnPlayer4Btn.Text = playerOff_text;
            this.SubOnPlayer4Btn.Enabled = false;
        }

        //This code is run when the fifth player button on the screen is clicked
        private void SubOnPlayer5Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer5Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer5Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer5Btn.Tag = playerOff_id;
            this.SubOnPlayer5Btn.Text = playerOff_text;
            this.SubOnPlayer5Btn.Enabled = false;
        }

        //This code is run when the sixth player button on the screen is clicked
        private void SubOnPlayer6Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer6Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer6Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer6Btn.Tag = playerOff_id;
            this.SubOnPlayer6Btn.Text = playerOff_text;
            this.SubOnPlayer6Btn.Enabled = false;
        }

        //This code is run when the seventh player button on the screen is clicked
        private void SubOnPlayer7Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer7Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer7Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer7Btn.Tag = playerOff_id;
            this.SubOnPlayer7Btn.Text = playerOff_text;
            this.SubOnPlayer7Btn.Enabled = false;
        }

        //This code is run when the eighth player button on the screen is clicked
        private void SubOnPlayer8Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer8Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer8Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer8Btn.Tag = playerOff_id;
            this.SubOnPlayer8Btn.Text = playerOff_text;
            this.SubOnPlayer8Btn.Enabled = false;
        }

        //This code is run when the ninth player button on the screen is clicked
        private void SubOnPlayer9Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer9Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer9Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer9Btn.Tag = playerOff_id;
            this.SubOnPlayer9Btn.Text = playerOff_text;
            this.SubOnPlayer9Btn.Enabled = false;
        }

        //This code is run when the tenth player button on the screen is clicked
        private void SubOnPlayer10Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer10Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer10Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer10Btn.Tag = playerOff_id;
            this.SubOnPlayer10Btn.Text = playerOff_text;
            this.SubOnPlayer10Btn.Enabled = false;
        }

        //This code is run when the eleventh player button on the screen is clicked
        private void SubOnPlayer11Btn_Click(object sender, EventArgs e)
        {
            //Get hold of details of the player being bought on from the 
            //button that has been clicked
            playerOn_id = Int32.Parse(this.SubOnPlayer11Btn.Tag.ToString());
            playerOn_text = this.SubOnPlayer11Btn.Text;

            //Update relevant buttons on other screens to reflect the substitution
            //including removing any colouring from previous cautions
            subOffButton.Tag = playerOn_id;
            subOffButton.Text = playerOn_text;
            subOffButton.BackColor = SystemColors.Control;
            sendOffButton.Tag = playerOn_id;
            sendOffButton.Text = playerOn_text;
            sendOffButton.BackColor = SystemColors.Control;
            cautionButton.Tag = playerOn_id;
            cautionButton.Text = playerOn_text;
            cautionButton.BackColor = SystemColors.Control;
            goalButton.Tag = playerOn_id;
            goalButton.Text = playerOn_text;
            goalButton.BackColor = SystemColors.Control;

            //Carry out the actual substitution within the database
            subPlayer();

            //Hide this screen to display the main Match screen
            this.Visible = false;

            //Add the subbed off player to the 'bench' and disable the relevant
            //button so the player cannot be subbed back on again
            this.SubOnPlayer11Btn.Tag = playerOff_id;
            this.SubOnPlayer11Btn.Text = playerOff_text;
            this.SubOnPlayer11Btn.Enabled = false;
        }

        //This code establishes a connection to the database
        private void connectToDatabase()
        {
            // Retrieve the connection string from the settings file.
            conString = @"Data Source=\Program Files\RefOffPDA\RefOffPDA_DB.sdf";

            // Open the connection using the connection string.
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //This code disconnects from the database
        private void closeDatabaseConnection()
        {
            conn.Close();
        }

        //This code is run when the 'Cancel' button is clicked
        private void CancelButton_Click(object sender, EventArgs e)
        {
            //Close this form to display the main Match screen
            this.Visible = false;
        }
    }
}